// packageJ/minprogramPay/minprogramPay.js
import logs from "../../utils/logs";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      options: options
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    let options = this.data.options;
    let obj = wx.getStorageSync('H5PayCallBackUrl')?wx.getStorageSync('H5PayCallBackUrl'):false;
    app._getNetWork({
      url: app.getNetAddresss("plugin.freelogin.frontend.third-party-pay.pay"),
      data: {
        app_id: options.aid,
        goods: options.goods,
        amount: options.am,
        out_trade_no: options.sn,
        other_uniacid: options.i,
        mobile: options.mobile,
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result == 0) {
          return app.tips(msg);
        }
        console.log('daaaaa', data)
        let payParams = data.config;
        wx.requestPayment({
          timeStamp: payParams.timestamp,
          nonceStr: payParams.nonceStr,
          package: payParams.package,
          signType: payParams.signType ? payParams.signType : "MD5",
          paySign: payParams.paySign,
          success: (res) => {
            // wx.navigateBack({
            //   delta: 1,
            // });
            if(obj){
              // 如果支付成功跳转到支付轮询页
              wx.redirectTo({
                url: "/packageI/web-view/h5View?diyUrl=success",
              })
            }
          },
          fail: (res) => {
            if(obj.failLink){
              wx.redirectTo({
                url: "/packageI/web-view/h5View?diyUrl=failLink",
                fail:(err)=>{
                  wx.navigateBack({
                    delta: 1,
                  });
                }
              })
            }else{
              wx.navigateBack({
                delta: 1,
              });
            }
            
            wx.showToast({
              title: "支付失败请稍后重试",
              icon: "none",
              duration: 2000,
            });
            logs.info('----------WXPay>requestPayment err-----------', err)
          },
        });
      }
    })
  },
})